package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mysql.wallet.SundayWalletRecord;
import org.itboys.mobile.service.SundayReportService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_报表_controller_web
 *
 */
@Controller
@RequestMapping("/sunday/web/report")
public class SundayReportController extends BaseController{


    @Autowired
    private SundayReportService reportService;

    /**
     * 产品销量报表主页
     * @param request
     * @param model
     * @param type 1单品销量，2分类销量，3品牌销量，4地区销量，5用户销量
     * @return
     */
    @RequestMapping("/product/index")
    public String product_index(HttpServletRequest request,
                                HttpServletResponse response,
                                Model model,
                                @RequestParam(value="type",required=false) Integer type){

        String url ="/sunday/report/product/index";
        if(type!=null) {
            if (type == 1) {
                url = "/sunday/report/product/product";
            } else if (type == 2) {
                url = "/sunday/report/product/category";
            } else if (type == 3) {
                url = "/sunday/report/product/brand";
            } else if (type == 4) {
                url = "/sunday/report/product/area";
            } else if (type == 5) {
                url = "/sunday/report/product/member";
            }
        }
        model.addAttribute("type",type);
        return url;
    }


    /**
     * 商品销量报表
     * @param request
     * @param provinceId 省ID
     * @param cityId  市ID
     * @param districtId 区ID
     * @param ct_start 开始时间
     * @param ct_end 结束时间
     * @param groupType 分组方式 1产品，2分类，3品牌，4门店，5用户，6省，7市，8区，
     * @param isDeal 是否只查询已经交易
     * @param isShop  是否只查门店
     * @return
     */
    @RequestMapping("/product/select")
    public void product_select(HttpServletRequest request,
                               HttpServletResponse response,
                               @RequestParam(value="provinceId",required=false) Long provinceId,
                               @RequestParam(value="cityId",required=false) Long cityId,
                               @RequestParam(value="districtId",required=false) Long districtId,
                               @RequestParam(value="ct_start",required=false) Date ct_start,
                               @RequestParam(value="ct_end",required=false) Date ct_end,
                               @RequestParam(value="groupType",required=false) Integer groupType,
                               @RequestParam(value="isDeal",required=false) Integer isDeal,
                               @RequestParam(value="isShop",required=false) Integer isShop){
        try {
          //  PageResult<SundayWalletRecordDto> result = cashService.select(request);
            PageResult result = reportService.getProductReportData(
                    request,
                    provinceId,
                     cityId,
                     districtId,
                     ct_start,
                     ct_end,
                     groupType,
                     isDeal,
                     isShop);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/sunday/web/report/product/select------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }
    }

    /**
     * 门店主页
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/shop/index")
    public String shop_index(HttpServletRequest request,
                                HttpServletResponse response,
                                Model model){

        String url ="/sunday/report/shop/index";
        return url;
    }
    /**
     * 佣金主页
     * @param request
     * @param response
     * @param model
     * @return
     */
    @RequestMapping("/reward/index")
    public String reward_index(HttpServletRequest request,
                             HttpServletResponse response,
                             Model model){

        String url ="/sunday/report/reward/index";
        return url;
    }

    /**
     * 佣金发放主页
     * @param request
     * @param response
     * @param ct_start
     * @param ct_end
     */
    @RequestMapping("/reward/select")
    public void reward_select(HttpServletRequest request,
                               HttpServletResponse response,
                               @RequestParam(value="ct_start",required=false) Date ct_start,
                               @RequestParam(value="ct_end",required=false) Date ct_end
                               ){
        try {
            PageResult result = reportService.getRewardReportData(request,false,
                    ct_start,
                    ct_end);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {

            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")){
                msg=e.getMessage();
            }else{
                msg= CommonConstants.ERROMSG;
                logger.error("/sunday/web/report/reward/select------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE,msg));
        }
    }

}
